<template>
	<view class="detail">
		<view class="title">{{newsdetail.title||'新闻标题新闻标题新闻标题新闻标题新闻标题'}}</view>
		<view class="info">
			<view class="author">编辑：{{newsdetail.author||'作者'}}</view>
			<view class="time">发布时间：{{newsdetail.posttime||'2024-07-17 10:35:00'}}</view>
		</view>
		<view class="content">
			<rich-text :nodes="newsdetail.content"></rich-text>
		</view>
		<view class="description">
			声明：本站内容均采集于腾讯新闻，如有侵权请联系管理进行整改删除。
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: null,
				newsdetail: {},
			};
		},
		onLoad(e) {
			// this.options = e;
			this.getNewsDetail(this.options);
		},
		methods: {
			getNewsDetail(options) {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/detail.php",
					data: this.options,
					success: res => {
						this.newsdetail = res.data;
						res.data.posttime = this.timestampToTime(res.data.posttime * 1000);
						res.data.content = res.data.content.replace(/<img/gi, '<img style="max-width:100%"');
						// console.log(res.data);
						this.saveHistory();
					}
				})
			},
			/* 时间戳转换为时间 */
			timestampToTime(timestamp) {
				timestamp = timestamp ? timestamp : null;
				let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let Y = date.getFullYear() + '-';
				let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
				let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
				let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
				let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
				return Y + M + D + h + m + s;
			},
			// 存储到历史记录
			saveHistory() {
				let historyArr = uni.getStorageSync('historyArr') || []
				let item = {
					id: this.newsdetail.id,
					classid: this.newsdetail.classid,
					picurl: this.newsdetail.picurl,
					looktime: this.timestampToTime(Date.now()),
					title: this.newsdetail.title,
				}
				let index = historyArr.findIndex(i => {
					return i.id == this.newsdetail.id
				})
				console.log(index);
				if (index >= 0) {
					historyArr.splice(index, 1)
				}
				historyArr.unshift(item)
				historyArr = historyArr.slice(0, 10) // 限制本地存储大小10条记录
				uni.setStorageSync('historyArr', historyArr)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding: 30rpx;

		.title {
			font-size: 46rpx;
			color: #333;
		}

		.info {
			background-color: #f6f6f6;
			padding: 20rpx;
			font-size: 25rpx;
			color: #666;
			display: flex;
			justify-content: space-between;
			margin: 40rpx 0;
		}

		.content {
			padding: 50rpx;

			// /deep/ img {
			// 	max-width: 100%;
			// }
		}

		.description {
			font-size: 26rpx;
			background: #fef0f0;
			padding: 20rpx;
			color: #f89898;
			line-height: 1.8em;
		}
	}
</style>